﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using AMM.Business;
using AMM.Business.Project;
using AMM.Business.Store;
using AMM.CustomClassLibrary;
using AMM.CustomWinComponents.Validator;
using AMM.Model;
using AMM.Model.Project;

namespace AMM.UserControls {
    public partial class ProductOutStoreOrderEditControl : BaseEditControl {

        private FlexBindingSource<ProductOutStoreOrder> _fbs;
        private ProductOutStoreOrderProcessor _productOutStoreOrderProcessor;

        public override object BaseEditModel {
            get {
                this._fbs.CurrentItem.ProductOutStoreOrderDetails = this.dgvProductEdit.GetDataSourceEx<ProductOutStoreOrderDetail>();
                return this._fbs.CurrentItem;
            }
            set {
                if (this._fbs == null) { this._fbs = new FlexBindingSource<ProductOutStoreOrder>(); }
                this._fbs.CurrentItem = (ProductOutStoreOrder)value;
            }
        }

        public ProductOutStoreOrderEditControl() {
            this.InitializeComponent();
        }

        private void BindProduct() {
            ProductSearchModel searchModel = new ProductSearchModel();
            ProductProcessor productProcessor = new ProductProcessor(int.MaxValue);
            List<ProductViewModel> productList = productProcessor.FetchProductViewModelList(searchModel);

            this.Product.DisplayMemberEx = "Code";
            this.Product.ValueMemberEx = "Key";
            this.Product.HeightEx = 300;
            this.Product.WidthEx = 400;
            this.Product.PageSizeEx = 20;
            this.Product.DataBind(new ProductViewModel(), x => new { x.ProductType, x.Code, x.Name, x.Remarks }, productList);
        }

        private void CreateFormBindings() {
            this._fbs.CreateBinding(this.cmbOutStoreType, x => x.SelectedValue, y => y.OutStoreTypeId);
            this._fbs.CreateBinding(this.tbRemarks, x => x.Text, y => y.Remarks);
            this._fbs.CreateBinding(this.tbCode, x => x.Text, y => y.Code);
        }

        private void BindValidation() {
            this.ValidatorForEdit.SetType(this.tbCode, ValidationType.Required | ValidationType.RegularExpression);
            this.ValidatorForEdit.SetRequiredMessage(this.tbCode, this.RequiredMessage);
            this.ValidatorForEdit.SetRegularExpression(this.tbCode, "^[A-Za-z0-9-]+$");
            this.ValidatorForEdit.SetRegularExpressionMessage(this.tbCode, "编码只能由字母，数字，连接线组成");

            this.ValidatorForEdit.SetType(this.cmbOutStoreType, ValidationType.Required);
            this.ValidatorForEdit.SetRequiredMessage(this.cmbOutStoreType, this.RequiredMessage);
        }

        private void BindDataGridView() {
            this.dgvProductEdit.DataSourceEx = this._fbs.CurrentItem.ProductOutStoreOrderDetails.ToList();
        }

        private void ProductOutStoreEditControl_Load(object sender, EventArgs e) {
            if (this._fbs == null) { this._fbs = new FlexBindingSource<ProductOutStoreOrder>(); }
            this._productOutStoreOrderProcessor = new ProductOutStoreOrderProcessor();

            this.BindBasics(this.cmbOutStoreType, BasicsType.ProductOutStoreType);
            this.BindProduct();
            this.BindDataGridView();
            this.CreateFormBindings();
            this.BindValidation();

            if (this._fbs.CurrentItem.ClassStatus == ClassStatus.Added) {
                string autoCode = CommonHelper.GenerateAutoCode("MO");
                int maxNumber = new ProductOutStoreOrderProcessor().GetTheMaxFlowNumber(autoCode);
                this.tbCode.Text = this._fbs.CurrentItem.Code = CommonHelper.GendrateSpecificCode(autoCode, maxNumber);
            }
        }        

        private void dgvProductEdit_CellEndEdit(object sender, DataGridViewCellEventArgs e) {
            if (this.dgvProductEdit.Columns[e.ColumnIndex].Name == "Quantity") {
                decimal storeValue = Convert.ToDecimal(this.dgvProductEdit.Rows[e.RowIndex].Cells["StoreQuantity"].Value);             
                decimal value = Convert.ToDecimal(this.dgvProductEdit[e.ColumnIndex, e.RowIndex].Value);
                this.dgvProductEdit.Rows[e.RowIndex].Cells["ErrMessage"].Value = storeValue < value ? "库存不足" : string.Empty;
            }
            if (this.dgvProductEdit.Columns[e.ColumnIndex].Name == "Product") {
                int productId = Convert.ToInt32(this.dgvProductEdit.Rows[e.RowIndex].Cells["Product"].Value);
                decimal storeValue = this._productOutStoreOrderProcessor.GetTheCurrentProductStoreTotal(productId);
                this.dgvProductEdit.Rows[e.RowIndex].Cells["StoreQuantity"].Value = storeValue.ToString("G0");
            }
        }
    }
}
